package mxw.suiye.autopydroid.core.app;

import android.annotation.SuppressLint;
import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.graphics.drawable.Drawable;
import android.os.Message;
import android.provider.Settings;

import com.alibaba.fastjson.JSONObject;

import java.util.List;
import java.util.Map;

import mxw.suiye.autopydroid.android.MyApp;
import mxw.suiye.autopydroid.util.MyHandler;
import mxw.suiye.autopydroid.util.ResultData;


/**安卓应用管理**/
public class App {
    private ApplicationInfo applicationInfo;
    private PackageInfo packageInfo;

    public App(String packageName) {
        try{
            packageInfo = MyApp.getPM().getPackageInfo(packageName, 0);
            applicationInfo = packageInfo.applicationInfo;
        } catch (Exception e){
            e.printStackTrace();
        }
    }

    String getAppName(){
        String result = "查找失败";
        try{
            result = applicationInfo.loadLabel(MyApp.getPM()).toString();

        }catch (Exception e){
            e.printStackTrace();
        }
        return result;
    }

    String getClassName(){
        return applicationInfo.className;
    }

    Drawable get(){
        return applicationInfo.loadIcon(MyApp.getPM());
    }

    String getVersionName(){
        return packageInfo.versionName;
    }

    int getVersionCode(){
        return packageInfo.versionCode;
    }


    public static void startApp(Message msg){
        JSONObject data = ((JSONObject) msg.obj).getJSONObject("data");

        String packageName = data.getString("packageName");
        PackageInfo pi;
        try {
            pi = MyApp.getPM().getPackageInfo(packageName, 0);
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
            return;
        }
        Intent resolveIntent  = new Intent(Intent.ACTION_MAIN, null);
        resolveIntent.addCategory(Intent.CATEGORY_LAUNCHER);
        resolveIntent.setPackage(pi.packageName);

        @SuppressLint("QueryPermissionsNeeded") List<ResolveInfo> apps = MyApp.getPM().queryIntentActivities(resolveIntent , 0);

        ResolveInfo ri = apps.iterator().next();
        if (ri != null ) {
            packageName = ri.activityInfo.packageName;
            String className = ri.activityInfo.name;
            Intent intent = new Intent(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_LAUNCHER);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setComponent(new ComponentName(packageName, className));
            MyApp.getContext().startActivity(intent);

            ResultData.reply("0");
            return;
        }
        ResultData.reply("-1");
    }

    public static void getAppName(Message msg) {
        JSONObject data = ((JSONObject) msg.obj).getJSONObject("data");

        String packageName = data.getString("packageName");

        String res = "查找失败";
        try{
            ApplicationInfo info = MyApp.getPM().getApplicationInfo(packageName, 0);
            res = info.loadLabel(MyApp.getPM()).toString();

        }catch (Exception e){
            e.printStackTrace();
        }
        ResultData.reply(res);
    }

    public static void startAcSetting(){
        if (MyHandler.accessibilityStatus()) {
            return;
        }
        Intent i = new Intent(Settings.ACTION_ACCESSIBILITY_SETTINGS);
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        MyApp.getContext().startActivity(i);
    }
}
